package org.nf.api;

import com.google.gson.Gson;
import org.nf.product.model.Product;
import org.nf.product.service.ProductService;
import org.nf.product.service.impl.ProductServiceImpl;
import org.nf.product.vo.PageVO;
import org.nf.web.annotation.Model;
import org.nf.web.annotation.RequestMapping;
import org.nf.web.annotation.RequestParam;
import org.nf.web.servlet.View;
import org.nf.web.servlet.view.JsonView;

import javax.servlet.http.HttpSession;
import java.util.List;


public class ProductController extends BaseController{
    private ProductService service = new ProductServiceImpl();
    /**
     * 查询全部产品
     */
    @RequestMapping("/listProduct")
    public View listProduct(@RequestParam("product") String product, HttpSession session){
        Product product1 = new Gson().fromJson(product, Product.class);
        List<Product> products = service.listProduct(product1);
        session.setAttribute("products",products);
        return new JsonView(success(products));
    }
    /**
     * 通过名称查询产品
     */
    @RequestMapping("/getProduct")
    public View getProduct(@RequestParam("productName") String productName){
        List<Product> products = service.selectProductName(productName);
        return new JsonView(success(products));
    }

    /**
     * 通过分页查询
     * @param
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/listProductPaging")
    public View listProductPaging(@RequestParam("page")int page,@RequestParam("limit")int limit){
        PageVO<List<Product>> listPageVO = service.listProductPage(page, limit);
        return new JsonView(success(listPageVO));
    }
}
